﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;

namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblAreaDAL:ImtblArea
    {
        HKEA_CMSEntities model;
        mtblArea area;
        public mtblAreaDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblAreaDAL GetInstance()
        {
            return new mtblAreaDAL();
        }
        public int InsertENT(mtblArea ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblAreas.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblArea> GetListByENT(mtblArea ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            var result = model.mtblAreas.Where(a => a.AreaUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.AreaUID > 0)
                result = result.Where(a => a.AreaUID == ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblArea> GetListByENT(mtblArea ent)
        {
            var result = model.mtblAreas.Where(a => a.AreaUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.AreaUID > 0)
                result = result.Where(a => a.AreaUID == ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblArea ent)
        {
            var result = model.mtblAreas.Where(a => a.AreaUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.AreaUID > 0)
                result = result.Where(a => a.AreaUID == ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblArea ent)
        {
            area = model.mtblAreas.Where(a=>a.AreaUID==ent.AreaUID).FirstOrDefault();
            if (area != null)
            {
                area.Area_Chi = ent.Area_Chi;
                area.Area_Eng = ent.Area_Eng;
                area.Deleted = ent.Deleted;
                area.LastModifyBy = ent.LastModifyBy;
                area.LastModifyDate = DateTime.Now;
                area.Seq = ent.Seq;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblArea ent)
        {
            area = model.mtblAreas.Where(a => a.AreaUID == ent.AreaUID).FirstOrDefault();
            if (area != null)
            {
                model.mtblAreas.DeleteObject(area);
            }
            return model.SaveChanges();
        }
    }
}
